import {sendError, sendSuccess} from "../../utils/responseHandler";
import sequelize from "../../../db";

export default async function handler(req, res) {
    if (req.method === 'GET') {
        try {
            const [results, metadata] = await sequelize.query(`
                SELECT
                  (SELECT COUNT(*) FROM db_works) AS total_jobs,
                  (SELECT AVG((min_salary + max_salary) / 2) FROM db_works) AS avg_salary,
                  (SELECT COUNT(*) FROM db_job_seekers) AS total_applicants;
            `);

            const stats = results[0] || {}; // 根据实际情况调整访问结果的方式
            sendSuccess(res, {
                totalJobs: stats.total_jobs,
                averageSalary:parseFloat(stats.avg_salary).toFixed(2),
                totalApplicants: stats.total_applicants
            });

        } catch (error) {
                sendError(res, error, 500);
        }
    } else {
        res.setHeader('Allow', ['GET']);
        res.status(405).end('Method Not Allowed');
    }
}
